<?php
/**
 * Created by PhpStorm.
 * User: luo
 * Date: 2018/12/30
 * Time: 23:30
 */

namespace app\commands;

use app\models\User;
use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionIndex(){
//        $permissions = Yii::$app->authManager->getPermissions();
//        print_r($permissions);
//        $roles = Yii::$app->authManager->getRoles();
//        print_r($roles);
        $users = User::findAll(['type'=>User::ADMIN]);
        foreach ($users as $v){
            print_r(Yii::$app->authManager->getRolesByUser($v->id));
        }
    }

    public function actionInit(){
        $permissions = [
            '*' => '全部权限',
//            '/admin/activity/*' => '活动全部权限',
            '/admin/article/*' => '文章全部权限',
//            '/admin/category/*' => '文章全部权限',
//            '/admin/channel/*' => '文章全部权限',
//            '/admin/feed/*' => '文章全部权限',
//            '/admin/home/*' => '文章全部权限',
//            '/admin/home/index' => '首页权限',
//            '/admin/job/*' => '职业全部权限',
//            '/admin/job-tag/*' => '职业关联全部权限',
//            '/admin/reply/*' => '评论全部权限',
//            '/admin/report/*' => '举报全部权限',
            '/admin/tag/search' => '标签/主题/话题搜索权限',
            '/admin/job/search' => '职业搜索权限',
            '/admin/user/select-data' => '选择用户数据',
            '/admin/user/virtual-user-list' => '虚拟用户数据',
            '/admin/channel/index' => '获取频道列表',
            '/admin/tag/list-by-hot' => '获取热门标签',
            '/admin/tag-article/*' => '文章关联全部权限',
//            '/admin/user/*' => '用户全部权限',
            '.' => '路由全部权限',

            'see_phone' => '能否查看手机号',
            'manage_admin' => '管理员操作'
        ];

        foreach ($permissions as $k=>$v){
            $this->editPermission($k, $v);
        }

        $roles = [
            '编辑' => '仅能管理文章',
            '运营' => '除手机号外，能查看所有页面',
            '超级管理员' => '拥有全部权限',
        ];
        foreach ($roles as $k=>$v){
            $this->editRole($k, $v);
        }

        $children = [
            '编辑' => ['/admin/article/*', '/admin/tag/search', '/admin/job/search', '/admin/user/select-data', '/admin/user/virtual-user-list', '/admin/channel/index', '/admin/tag/list-by-hot', '/admin/tag-article/*'],
            '运营' => ['.'],
            '超级管理员' => ['*']
        ];

        foreach ($children as $k => $v){
            $role = Yii::$app->authManager->getRole($k);
            Yii::$app->authManager->removeChildren($role);

            foreach ($v as $name) {
                $permission = Yii::$app->authManager->getPermission($name);
                Yii::$app->authManager->addChild($role, $permission);
            }
        }

//        $users = User::findAll(['type'=>User::ADMIN]);
//        foreach ($users as $v){
//            Yii::$app->authManager->revokeAll($v->id);
//            if($v->username == 'admin'){
//                Yii::$app->authManager->assign(
//                    Yii::$app->authManager->getRole('超级管理员'), $v->id);
//            }else{
//                Yii::$app->authManager->assign(
//                    Yii::$app->authManager->getRole('运营'), $v->id);
//            }
//        }
    }

    private function editPermission($name, $description){
        $permission = Yii::$app->authManager->getPermission($name);
        if($permission){
            $permission->name = $name;
            $permission->description = $description;
            Yii::$app->authManager->update($name,$permission);
        }else{
            $permission = Yii::$app->authManager->createPermission($name);
            $permission->description = $description;
            Yii::$app->authManager->add($permission);
        }
    }

    private function deletePermission($name){
        $permission = Yii::$app->authManager->getPermission($name);
        Yii::$app->authManager->remove($permission);
    }

    private function editRole($name, $description){
        $role = Yii::$app->authManager->getRole($name);
        if ($role) {
            $role->name = $name;
            $role->description = $description;
            Yii::$app->authManager->update($name, $role);
        }else{
            $role = Yii::$app->authManager->createRole($name);
            $role->description = $description;
            Yii::$app->authManager->add($role);
        }
    }

    private function deleteRole($name){
        $role = Yii::$app->authManager->getRole($name);
        Yii::$app->authManager->remove($role);
    }

}